﻿<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages.Administration, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" MasterPageFile="~/_admin/popup.master" %>
<%@ Import Namespace="System.Web.Configuration"%>

<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="Microsoft.SharePoint.Administration" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Microsoft.Win32" %>

<script runat="server"> 
    
    /**
     * This form changes the setting specified by mode, which is read from the request url.
     * The modes are
     * lic: license
     * mai: email server
     * addr: email address
     * */
    
    /// <summary>
    /// <para>The setting to change.</para>
    /// <para>lic: license</para>
    /// <para>con: Arrow database connection string</para>
    /// <para>mai: email server</para>
    /// <para>addr: email address</para>
    /// </summary>
    private string mode;

    const string MODE_QS = "mode";

    const string MODE_LIC = "lic";
    const string MODE_MAI = "mai";
    const string MODE_ADDR = "addr";
    
    const string LICENSE_HEADER = "<H3 class='ms-standardheader'>Edit Arrow License Key</H3>";
    const string EMAIL_HEADER = "<H3 class='ms-standardheader'>Edit Email Settings</H3>";

    const string JS_CONFIRM = "return confirm('Please confirm that you want to edit the property.')";

    const string ARROW_LIC_PROP = "WindwardArrowLicense";

    const string ARROW_REG_KEY_HKLM = "Software\\Windward Studios\\Arrow";
    const string EMAIL_SVR_REG_KEY = "email-server";
    const string EMAIL_ADDR_REG_KEY = "email-address";

    const string NA = "(None found)";

    protected void Page_Load(object sender, EventArgs e)
    {
        // load the mode
        if (Request.QueryString[MODE_QS] != null && Request.QueryString[MODE_QS] != String.Empty)
            mode = Request.QueryString[MODE_QS];
        else
        {
            lblMsg.Text = "Setting mode not found. Please contact Windward support.";
            return;
        }
        
        // initialize the form for the setting
        if (!Page.IsPostBack)
        {
            switch (mode)
            {
                case MODE_LIC:
                    headerLabel.Text = LICENSE_HEADER;
                    initControls();
                    break;
                case MODE_MAI:
                    headerLabel.Text = EMAIL_HEADER;
                    initControls();
                    break;
                case MODE_ADDR:
                    headerLabel.Text = EMAIL_HEADER;
                    initControls();
                    break;
                default:
                    headerLabel.Text = "Setting mode not recognized.  Please contact Windward support.";
                    return;
            }
        }
    }

    /// <summary>
    /// Loads the setting's current value and hooks up the form controls.
    /// </summary>
    private void initControls()
    {
        LoadSetting();
        lnkSave1.Attributes.Add("onclick", JS_CONFIRM);
        lnkSave2.Attributes.Add("onclick", JS_CONFIRM);
        btnSave1.Attributes.Add("onclick", JS_CONFIRM);
        btnSave2.Attributes.Add("onclick", JS_CONFIRM);
    }

    /// <summary>
    /// Loads the current value of the setting specified by mode.
    /// </summary>
    private void LoadSetting()
    {
        switch (mode)
        {
            case MODE_LIC:
                settingTextBox.Text = getLicense();
                break;
            case MODE_MAI:
                settingTextBox.Text = getEmailServer();
                break;
            case MODE_ADDR:
                settingTextBox.Text = getEmailAddress();
                break;
            default:
                break;
        }
    }
    
    /// <summary>
    /// Saves the property specified by mode, using the value from settingTextBox.
    /// </summary>
    private void Save()
    {
        try
        {
            switch (mode)
            {
               case MODE_LIC:
                    setLicense();
                    break;
                case MODE_MAI:
                    setEmailServer();
                    break;
                case MODE_ADDR:
                    setEmailAddress();
                    break;
                default:
                    break;
            }
            lblMsg.Text = "Setting saved.";
        }
        catch(Exception e)
        {
            lblMsg.Text = "Error saving changes.  Error message: " + e.Message + "\n" + e.StackTrace;
        }
    }

    protected string getLicense()
    {
        return (string)SPFarm.Local.Properties[ARROW_LIC_PROP];
    }

    private void setLicense()
    {
        SPFarm farm = SPFarm.Local;
        farm.Properties[ARROW_LIC_PROP] = settingTextBox.Text;
        farm.Update();
    }

    protected string getEmailServer()
    {
        RegistryKey key = Registry.LocalMachine.OpenSubKey(ARROW_REG_KEY_HKLM);
        if (key != null)
            return (string)key.GetValue(EMAIL_SVR_REG_KEY, "localhost");
        return NA;
    }

    private void setEmailServer()
    {
        RegistryKey key = Registry.LocalMachine.CreateSubKey(ARROW_REG_KEY_HKLM);
        if (key != null && settingTextBox.Text != null)
            key.SetValue(EMAIL_SVR_REG_KEY, settingTextBox.Text.Trim());
    }

    protected string getEmailAddress()
    {
        RegistryKey key = Registry.LocalMachine.OpenSubKey(ARROW_REG_KEY_HKLM);
        if (key != null)
            return (string)key.GetValue(EMAIL_ADDR_REG_KEY, "you@domain.com");
        return NA;
    }

    private void setEmailAddress()
    {
        RegistryKey key = Registry.LocalMachine.CreateSubKey(ARROW_REG_KEY_HKLM);
        if (key != null && settingTextBox.Text != null)
            key.SetValue(EMAIL_ADDR_REG_KEY, settingTextBox.Text.Trim());
    }

    protected void Save_Click(object sender, ImageClickEventArgs e)
    {
        Save();
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        Save();
    }
</script>

<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
  <div>
        <table cellpadding="0" cellspacing="0" style="height: 410; width: 100%">
            <tr>
                <td valign="top" style="height: 10%">
                    <table class="ms-toolbar" width="100%" style="padding-left: 3px; padding-right: 5px;"
                        cellspacing="0" cellpadding="2">
                        <tr>
                            <td width="10%">
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="0" class="ms-toolbar">
                                            <a href="javascript:__doPostBack(&#39;ctl00$PlaceHolderMain$ProfileSave&#39;,&#39;&#39;)"
                                                title="Save">
                                                <asp:ImageButton ID="btnSave1" AlternateText="Save and Close" ImageUrl="/_layouts/images/save.gif"
                                                    BorderWidth="0" runat="server" OnClick="Save_Click" /></a>
                                        </td>
                                        <td class="" width="100%" nowrap>
                                            <asp:LinkButton ID="lnkSave1" runat="server" CssClass="ms-toolbar" ToolTip="Save"
                                                OnClick="Save_Click">Save</asp:LinkButton>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td class="ms-separator" width="2%">
                                |
                            </td>
                            <td>
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="100%" nowrap>
                                            <a class="ms-toolbar" href="javascript:window.close();">Close</a>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td valign="top" style="height: 100%;">
                    <table cellpadding="5" cellspacing="5">
                        <tr>
                            <td>
                                <asp:Label ID="headerLabel" runat="server" Text=""></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <table>
                                    <tr>
                                        <td class="ms-authoringcontrols">
                                            <asp:Label ID="settingLabel" runat="server" CssClass="ms-authoringcontrols" Text=""></asp:Label>
                                        </td>
                                        <td>
                                            <asp:TextBox ID="settingTextBox" runat="server" Columns="35" CssClass="ms-input" Width="350" Height="150" TextMode="MultiLine" Wrap="false"></asp:TextBox>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label runat="server" ID="lblMsg" Width="380px" class="ms-descriptiontext ms-inputformdescription" ></asp:Label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>           
            <tr>
                <td valign="bottom" style="height: 10%">
                    <table id="Table2" class="ms-toolbar" width="100%" style="padding-left: 3px; padding-right: 5px;"
                        cellspacing="0" cellpadding="2">
                        <tr>
                            <td width="10%">
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="0" class="ms-toolbar">
                                            <a href="javascript:__doPostBack(&#39;ctl00$PlaceHolderMain$ProfileSave&#39;,&#39;&#39;)"
                                                title="Save and Close">
                                                <asp:ImageButton ID="btnSave2" OnClick="Save_Click" AlternateText="Save"
                                                    ImageUrl="/_layouts/images/save.gif" BorderWidth="0" runat="server" /></a>
                                        </td>
                                        <td width="100%" nowrap>
                                            <asp:LinkButton ID="lnkSave2" OnClick="Save_Click" CssClass="ms-toolbar"
                                                runat="server" ToolTip="Save">Save</asp:LinkButton>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td class="ms-separator" width="1%">
                                |
                            </td>
                            <td>
                                <table cellspacing="1" cellpadding="0" border="0">
                                    <tr>
                                        <td width="100%" nowrap>
                                            <a class="ms-toolbar" href="javascript:window.close();">Close</a>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
</asp:Content>

<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    Modify Arrow Setting
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"
    runat="server">
    Modify Arrow Setting
</asp:Content>
